<template>
  <div class='com-container'>
    <div class='com-chart' ref='hot_ref'></div>
    <span class="iconfont arr-left" @click="toLeft" :style="comStyle">&#xe6ef;</span>
    <span class="iconfont arr-right" @click="toRight" :style="comStyle">&#xe6ed;</span>
    <span class="cat-name" :style="comStyle">{{ catName }}</span>
  </div>
</template>

<script>
import axios from 'axios'
export default {
  data () {
    return {
      chartInstance: null,
      allData: null,
      currentIndex: 0, // 进行切换一级类别的索引
      titleFontSize: 0,
    }
  },
  computed: {
    catName(){
        if(!this.allData) return ''
        return this.allData[this.currentIndex].name
    },
    comStyle (){
        return {
            fontSize: this.titleFontSize + 'px'
        }
    }
  },
  mounted () {
    this.initChart()
    this.getData()
    window.addEventListener('resize', this.screenAdapter)
    this.screenAdapter()
  },
  destroyed () {
    window.removeEventListener('resize', this.screenAdapter)
  },
  methods: {
    async initChart () {
      this.chartInstance = this.$echarts.init(this.$refs.hot_ref, 'chalk')
      const initOption = {
        title: {
            text: '| 热销商品的占比',
            top: 20,
            left: 20
        },
        legend: {
            top: '15%',
            icon: 'circle',
        },
        tooltip: {
            show: true,
            formatter: arg => {
                const thirdCategory = arg.data.children
                // 计算出所有的三级分类的数值得总和
                let total = 0
                thirdCategory.forEach(item => {
                    total += item.value
                })
                let retStr = ''
                thirdCategory.forEach(item => {
                    retStr += `${item.name}: ${parseInt(item.value / total * 100) + '%'}<br>`
                })
                return retStr
            }
        },
        series: [
            {
                type: 'pie',
                label: {
                    show: false
                },
                emphasis: { // 鼠标移入饼图时，饼图高亮,同时进行显示需要的东西
                    label: {
                        show: true
                    },
                    labelLine: {
                        show: false
                    }
                }
            }
        ]
      }
      this.chartInstance.setOption(initOption)
    },
    async getData () {
      // 获取服务器的数据, 对this.allData进行赋值之后, 调用updateChart方法更新图表
      const { data: ret } = await this.$http.get('hot')
      this.allData = ret
      console.log(this.allData)
      this.updateChart()
    },
    updateChart () {
      // 处理图表需要的数据
      const legendData = this.allData[this.currentIndex].children.map(item => item.name)
      const seriesData = this.allData[this.currentIndex].children.map(item => ({ name: item.name, value: item.value, children: item.children })) // item.children是将这里的三层的数据进行传递到数据中，方便tooltip的使用
      // 图例的数据
      const dataOption = {
        legend: {
          data: legendData
        },
        series: [
          {
            data: seriesData
          }
        ]
      }
      this.chartInstance.setOption(dataOption)
    },
    screenAdapter () {
      this.titleFontSize = this.$refs.hot_ref.offsetWidth / 100 * 3.6
      const adapterOption = {
        title: {
          textStyle: {
            fontSize: this.titleFontSize
          }
        },
        legend: {
          itemWidth: this.titleFontSize / 2,
          itemHeight: this.titleFontSize / 2,
          itemGap: this.titleFontSize / 2,
          textStyle: {
            fontSize: this.titleFontSize /2
          },
        },
        series: [
          {
            radius: this.titleFontSize * 4.5,
            center: ['50%', '60%']
          }
        ]
      }
      this.chartInstance.setOption(adapterOption)
      this.chartInstance.resize()
    },
    toLeft() {
        this.currentIndex--
        if (this.currentIndex < 0) {
            this.currentIndex = this.allData.length - 1
        }
        this.updateChart()
    },
    toRight() {
        this.currentIndex++
        if (this.currentIndex >= this.allData.length) {
            this.currentIndex = 0
        }
        this.updateChart()
    }
  }
}
</script>

<style lang='less' scoped>
.arr-left{
    position: absolute;
    left: 10%;
    top: 50%;
    transform: translateY(-50%); // 向上移动自生高度的一半
    cursor: pointer;
    color: white;
}
.arr-right{
    position: absolute;
    right: 10%;
    top: 50%;
    transform: translateY(-50%);
    cursor: pointer;
    color: white;
}
.cat-name{
    position: absolute;
    left: 80%;
    bottom: 20px;
    color: white;
}
</style>